﻿
// InjectorDlg.h: 头文件
//

#pragma once
#include "CProcessDlg.h"
#include "CSelectImage.h"
#include "CSelectInject.h"
#include "CtrlCode.h"
#include "CShellcodeDlg.h"
#include "CSelectInject2.h"
#include "Prototypes.h"


#ifndef PAGE_SIZE
#define PAGE_SIZE   0x1000
#endif

#ifdef _WIN64
#define INJECT_ADDRESS      0x98000000
#else
#define INJECT_ADDRESS      0x10000
#endif

typedef ULONG_PTR   LongPtr, * PLongPtr;

// CInjectorDlg 对话框
class CInjectorDlg : public CDialogEx
{
// 构造
public:
	CInjectorDlg(CWnd* pParent = nullptr);	// 标准构造函数

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_INJECTOR_DIALOG };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV 支持


// 实现
protected:
	HICON m_hIcon;

	// 生成的消息映射函数
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	DECLARE_MESSAGE_MAP()

protected:
    CProcessDlg m_ProcessDlg;
    CSelectImage m_SelectImageDlg;
    CSelectInject m_SelectInjectDlg;
    CSelectInject2 m_SelectInjectDlg2;
    CShellcodeDlg m_ShellcodeDlg;
    DWORD m_dwProcessId;
    CListCtrl m_ModuleList;
    CString m_strDllPath;
    CMFCEditBrowseCtrl m_DllBrowseCtrl;
    CString m_strProcessImage;
    PINJECT_CONFIG m_pInjectConfig;
    PSHELLCODE_CONFIG m_pShellCodeConfig;
    SHELLCODE_INJECT m_CodeInjectType;
    MODULE_INJECT m_ModuleInjectType;
    LPVOID m_lpCodeAddress;
    HWND m_hMainWindow;
    PVOID m_pSetConsoleCtrlHandler;

protected:
    PVOID GetFunctionAddress(const char* DllName, const char* FunctionName);
    DWORD GetImageSize(IN LPVOID PeBase);
    void SetImageBase(IN LPVOID PeBase, IN LongPtr ImageBase);
    LPVOID RelocateImage(IN LPVOID PeBase, IN DWORD ImageSize, IN LongPtr ImageBase);
    BOOL GetRvaOfTextAndSize(IN LPVOID PeBase, IN OUT PDWORD RvaOfText, IN OUT PDWORD SizeOfText);
    DWORD GetRvaOfEntryPoint(IN LPVOID PeBase);

protected:
    virtual BOOL PreTranslateMessage(MSG* pMsg);

protected:
    static BOOL CALLBACK EnumWindowsProcAdapter(IN HWND hwnd, IN LPARAM lParam);
    BOOL CALLBACK EnumWindowsProc(IN HWND hwnd, IN LPARAM lParam);

protected:
    void InitializeModuleList();
    void RefreshModuleList();
    void RemoteThreadInject();
    void RemoteThreadInjectX86();
    void UserApcInject();
    void HijackInject();
    void MessageHookInject(IN LPCSTR FunctionName);
    void PeInject();

protected:
    BOOL PreRemoteThreadInjectCode(OUT PHANDLE hProcess);
    void PostRemoteThreadInjectCode(IN HANDLE hProcess, IN PVOID Code, IN SIZE_T Len);
    BOOL PreEarlyBirdInjectCode(OUT PHANDLE hProcess, OUT PHANDLE hThread);
    void PostEarlyBirdInjectCode(IN HANDLE hProcess, IN HANDLE hThread, IN PVOID Code, IN SIZE_T Len);
    BOOL PreCtrlInjectCode(OUT PHANDLE hProcess);
    void PostCtrlInjectCode(IN HANDLE hProcess, IN PVOID Code, IN SIZE_T Len);

public:
    afx_msg void OnBnClickedBtnTraversemodules();
    afx_msg void OnBnClickedBtnSelectprocess();
    afx_msg LRESULT OnRecvProcessId(WPARAM wParam, LPARAM lParam);
    afx_msg LRESULT OnRecvProcessImage(WPARAM wParam, LPARAM lParam);
    afx_msg LRESULT OnRecvInjectConfig(WPARAM wParam, LPARAM lParam);
    afx_msg LRESULT OnRecvShellCodeConfig(WPARAM wParam, LPARAM lParam);
    afx_msg LRESULT OnRecvShellCodeType(WPARAM wParam, LPARAM lParam);
    afx_msg LRESULT OnSendCodeInjectAddress(WPARAM wParam, LPARAM lParam);
    afx_msg void OnDropFiles(HDROP hDropInfo);
    afx_msg void OnBnClickedBtnInject();
    afx_msg void OnNMRClickListModule(NMHDR* pNMHDR, LRESULT* pResult);
    afx_msg void OnRightmenuRefreshlist();
    afx_msg void OnRightmenuRemovemodule();
    afx_msg void OnBnClickedBtnShellcodeinject();
};
